(function (vc, vm) {
    vc.extends({
        data: {
            editShopVipLevelInfo: {
                vipLevelId: '',
                levelName: '',
                vipLevel: '',
                remark: '',
                goldCostMoney: '1',
                integralCostMoney: '1',
                couponValues: [],
                vipLevels: []
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            vc.on('editShopVipLevel', 'openEditShopVipLevelModal', function (_params) {
                $that.refreshEditShopVipLevelInfo();
                $('#editShopVipLevelModel').modal('show');
                console.log("测试123")
                console.log(_params)
                vc.getDict('s_shop_vip_level', "vip_level", function (_data) {
                    $that.editShopVipLevelInfo.vipLevels = _data;
                });
                vc.copyObject(_params, $that.editShopVipLevelInfo);
                $that.editShopVipLevelInfo.couponValues = _params.couponConfDtos;
                $that.editShopVipLevelInfo.shopId = vc.getCurrentCommunity().shopId;
            });
            vc.on('chooseCouponShopPool', 'chooseCouponShopPool', function (_couponShopPool) {
                $that._addEditTitleValue(_couponShopPool);
            });
        },
        methods: {
            editShopVipLevelValidate: function () {
                return vc.validate.validate({
                    editShopVipLevelInfo: $that.editShopVipLevelInfo
                }, {
                    'editShopVipLevelInfo.levelName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "会员名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "会员名称太长"
                        },
                    ],
                    'editShopVipLevelInfo.vipLevel': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "会员等级不能为空"
                        },
                        {
                            limit: "num",
                            param: "",
                            errInfo: "会员等级错误"
                        },
                    ],
                    'editShopVipLevelInfo.goldCostMoney': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "优惠卷赠送不能为空"
                        },
                        {
                            limit: "money",
                            param: "",
                            errInfo: "优惠卷赠送错误"
                        },
                    ],
                    'editShopVipLevelInfo.integralCostMoney': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "积分赠送不能为空"
                        },
                        {
                            limit: "money",
                            param: "",
                            errInfo: "积分赠送错误"
                        },
                    ],
                    'editShopVipLevelInfo.remark': [
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "说明太长"
                        },
                    ],
                    'editShopVipLevelInfo.vipLevelId': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "主键不能为空"
                        }
                    ]
                });
            },
            editShopVipLevel: function () {
                if (!$that.editShopVipLevelValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                var isFalse = false;
                $that.editShopVipLevelInfo.couponValues.forEach(item => {
                    if (item.couponCount == 0) {
                        vc.toast(item.couponName + '优惠卷个数不能为空');
                        isFalse = true;
                    }
                    if (item.couponCount > item.stock) {
                        vc.toast(item.couponName + '优惠卷库存不足');
                        isFalse = true;
                    }
                });
                if (isFalse) {
                    return;
                }
                $that.editShopVipLevelInfo.shopId = vc.getCurrentCommunity().shopId;
                vc.http.apiPost(
                    '/shopVipUser/updateShopVipLevel',
                    JSON.stringify($that.editShopVipLevelInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#editShopVipLevelModel').modal('hide');
                            vc.emit('shopVipLevelManage', 'listShopVipLevel', {});
                            vc.toast("修改成功");
                            return;
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            _goEditBack: function () {
                vc.emit('shopVipLevelManage', 'listShopVipLevel', {});
            },
            _openEditChooseCouponShopPool: function () {
                vc.emit('chooseCouponShopPool', 'openChooseCouponShopPoolModel', {});
            },
            _addEditTitleValue: function (_couponShopPool) {
                var _temCounponShopPool = [];
                $that.editShopVipLevelInfo.couponValues.forEach(item => {
                    if (item.spId == _couponShopPool.spId) {
                        item.couponCount = Number(item.couponCount) + 1;
                        _temCounponShopPool.push(item);
                    } else {
                        _temCounponShopPool.push(item);
                    }
                });
                console.log(_temCounponShopPool);
                if (_temCounponShopPool.length > 0) {
                    $that.editShopVipLevelInfo.couponValues = _temCounponShopPool;
                } else {
                    $that.editShopVipLevelInfo.couponValues.push(_couponShopPool);
                }
            },
            _checkEditStock: function (_coupon) {
                if (_coupon.couponCount > _coupon.stock) {
                    vc.toast('优惠卷库存不足');
                    return;
                }
            },
            _deleteEditTitleValue: function (_spId) {
                console.log(_spId);
                let _newTitleValues = [];
                let _tmpTitleValues = $that.editShopVipLevelInfo.couponValues;
                _tmpTitleValues.forEach(item => {
                    if (item.spId != _spId) {
                        _newTitleValues.push(item)
                    }
                });
                $that.editShopVipLevelInfo.couponValues = _newTitleValues;
            },
            refreshEditShopVipLevelInfo: function () {
                $that.editShopVipLevelInfo = {
                    vipLevelId: '',
                    levelName: '',
                    vipLevel: '',
                    remark: '',
                    goldCostMoney: '1',
                    integralCostMoney: '1',
                    couponValues: [],
                    vipLevels: []
                }
            }
        }
    });
})(window.vc, window.$that);
